package com.bolovan.infovaluta.bean;

import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

import javax.jdo.annotations.PersistenceCapable;

import com.bolovan.infovaluta.reader.ExchangeReader;
import com.google.gson.Gson;

@PersistenceCapable
public class ExchangeRate {

	private ExchangeReader reader;
	private Set<ExchangeRateEntry> currencyMap = new TreeSet<>();

	public ExchangeRate(ExchangeReader reader) throws IOException {
		this.reader = reader;
		reader.buildRateMap(this);
	}

	public Set<ExchangeRateEntry> getCurrencyMap() {
		return currencyMap;
	}

	public void addToMap(ExchangeRateEntry rate) {
		rate.setSeller(this.reader.getSeller());
		rate.setDate(this.reader.getDate());
		rate.setParsingDate(this.reader.getParsingDate());
		currencyMap.add(rate);
	}

	public ExchangeReader getReader() {
		return reader;
	}

	public void setReader(ExchangeReader reader) {
		this.reader = reader;
	}

	@Override
	public String toString() {
		Gson gson = new Gson();
		return "RateMap [currencyMap=" + gson.toJson(currencyMap) + "]";
	}

}
